package com.gzyj.web.api.sys;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.ResponseMsg;
import com.gzyj.data.web.sys.JgParam;
import com.gzyj.pojo.anbiao.AnbiaoVehicle;
import com.gzyj.pojo.web.dept.AnbiaoOrganization;
import com.gzyj.pojo.web.dept.BladeDept;
import com.gzyj.pojo.web.dept.GzyjDeptTree;
import com.gzyj.web.mapper.dept.AnbiaoOrganizationMapper;
import com.gzyj.web.mapper.dept.BladeDeptMapper;
import com.gzyj.web.mapper.veh.AnbiaoVehicleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/sys/jggl")
public class JgglApi {

    @Autowired
    private BladeDeptMapper bladeDeptMapper;

    @Autowired
    private AnbiaoVehicleMapper anbiaoVehicleMapper;

    @Autowired
    private AnbiaoOrganizationMapper anbiaoOrganizationMapper;

    public List<GzyjDeptTree> buildTree() {
        GzyjDeptTree p = new GzyjDeptTree();
        p.setParentId(58L);
        List<GzyjDeptTree> rootNodes = bladeDeptMapper.getTree(p);
        buildChildren(rootNodes);
        return rootNodes;
    }

    private void buildChildren(List<GzyjDeptTree> nodes) {
        for (GzyjDeptTree node : nodes) {
            GzyjDeptTree p = new GzyjDeptTree();
            p.setParentId(node.getId());
            List<GzyjDeptTree> children = bladeDeptMapper.getTree(p);
            node.setChildren(children);
            if (children.size() == 0) {
                Integer vehCount = anbiaoVehicleMapper.selectCount(new QueryWrapper<AnbiaoVehicle>().eq("dept_id", node.getId()));
                node.setVehCnt(vehCount);
            }
            buildChildren(children);
        }
    }

    @PostMapping("tree")
    public ResponseMsg<List<GzyjDeptTree>> getTree(GzyjDeptTree param) {
        List<GzyjDeptTree> tree = buildTree();
        return new ResponseMsg<>(200, "success", tree);
    }

    @PostMapping("getZFTree")
    public ResponseMsg<List<GzyjDeptTree>> getZFTree() {
        // 获取所有节点
        List<GzyjDeptTree> allNodes = bladeDeptMapper.getZFTree();
        // 根节点列表
        List<GzyjDeptTree> rootNodes = new ArrayList<>();
        for (GzyjDeptTree node : allNodes) {
            if (node.getParentId() == 5446) {
                rootNodes.add(node);
            }
        }
        // 递归构建树
        for (GzyjDeptTree rootNode : rootNodes) {
            buildChildren(rootNode, allNodes);
        }
        return new ResponseMsg<>(200, "success", rootNodes);
    }

    private void buildChildren(GzyjDeptTree parentNode, List<GzyjDeptTree> allNodes) {
        List<GzyjDeptTree> children = new ArrayList<>();
        for (GzyjDeptTree node : allNodes) {
            if (node.getParentId() != null && node.getParentId().equals(parentNode.getId())) {
                children.add(node);
                buildChildren(node, allNodes);
            }
        }
        if (!children.isEmpty()) {
            parentNode.setChildren(children);
        }

    }

    @PostMapping("update")
    public ResponseMsg<Object> update(JgParam param) {

        AnbiaoOrganization organization = anbiaoOrganizationMapper.selectOne(new QueryWrapper<AnbiaoOrganization>().eq("dept_id", param.getId()));
        organization.setDeptName(param.getDeptName());
        organization.setJigoufuzeren(param.getJigoufuzeren());
        organization.setJigoubianma(param.getJigoubianma());
        organization.setLianxidianhua(param.getLianxidianhua());
        organization.setXiangxidizhi(param.getXiangxidizhi());
        List<String> zfs = param.getZgbmSelected();
        if (CollectionUtil.isNotEmpty(zfs)) {
            if (zfs.size() == 1) {
                organization.setProvince(zfs.get(0));
                organization.setCity(null);
                organization.setCountry(null);
            } else if (zfs.size() == 2) {
                organization.setProvince(zfs.get(0));
                organization.setCity(zfs.get(1));
                organization.setCountry(null);
            } else if (zfs.size() == 3) {
                organization.setProvince(zfs.get(0));
                organization.setCity(zfs.get(1));
                organization.setCountry(zfs.get(2));
            }
        }

        anbiaoOrganizationMapper.updateById(organization);

        BladeDept bladeDept = bladeDeptMapper.selectById(param.getId());

        bladeDept.setDeptName(param.getDeptName());
        bladeDept.setFullName(param.getDeptName());
        if (param.getParentId() != null) {
            bladeDept.setParentId(param.getParentId().longValue());
        }

        bladeDeptMapper.updateById(bladeDept);

        return new ResponseMsg<>(200, "success");
    }

    @PostMapping("add")
    public ResponseMsg<Object> add(JgParam param) {
        BladeDept bladeDept = new BladeDept();
        bladeDept.setDeptName(param.getDeptName());
        bladeDept.setFullName(param.getDeptName());
        bladeDept.setIsDeleted(0);
        bladeDept.setExtendType("机构");
        bladeDept.setParentId(param.getParentId().longValue());
        bladeDeptMapper.insert(bladeDept);
        AnbiaoOrganization organization = new AnbiaoOrganization();
        organization.setDeptId(bladeDept.getId() + "");
        organization.setDeptName(param.getDeptName());
        organization.setJigoufuzeren(param.getJigoufuzeren());
        organization.setJigoubianma(param.getJigoubianma());
        organization.setLianxidianhua(param.getLianxidianhua());
        organization.setXiangxidizhi(param.getXiangxidizhi());
        List<String> zfs = param.getZgbmSelected();
        if (CollectionUtil.isNotEmpty(zfs)) {
            if (zfs.size() == 1) {
                organization.setProvince(zfs.get(0));
                organization.setCity(null);
                organization.setCountry(null);
            } else if (zfs.size() == 2) {
                organization.setProvince(zfs.get(0));
                organization.setCity(zfs.get(1));
                organization.setCountry(null);
            } else if (zfs.size() == 3) {
                organization.setProvince(zfs.get(0));
                organization.setCity(zfs.get(1));
                organization.setCountry(zfs.get(2));
            }
        }

        anbiaoOrganizationMapper.insert(organization);

        return new ResponseMsg<>(200, "success");

    }

    @PostMapping("del")
    public ResponseMsg<Object> del(JgParam param) {
        bladeDeptMapper.deleteById(param.getId());

        anbiaoOrganizationMapper.delete(new QueryWrapper<AnbiaoOrganization>().eq("dept_id", param.getId()));
        return new ResponseMsg<>(200, "success");
    }


    @PostMapping("list")
    public ResponseMsg<List<AnbiaoOrganization>> jglist() {
        List<AnbiaoOrganization> list = anbiaoOrganizationMapper.selectList(new QueryWrapper<AnbiaoOrganization>().eq("isdelete", 0).eq("jigouleixing", "qiye"));
        return new ResponseMsg<>(200, "success", list);
    }

}
